package com.gjzt.business.contract.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.eastdigit.system.model.BusinessException;
import com.gjzt.business.contract.service.ContractService;

@Controller
@RequestMapping(value = "/contract")
public class ContractController {

    @Autowired
    private ContractService contractService;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject queryContracts(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(contractService.findContracts(reqBean));
    }

    /**
     * 合同树
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/contractTree")
    @ResponseBody
    public JSONObject contractTree(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(contractService.contractTree(reqBean));
    }

    
    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/{contractId}/info")
    @ResponseBody
    public JSONObject findContract(@PathVariable String contractId) {
        return Response.response(contractService.findContractWidthCustomer(contractId));
    }
    
    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/{contractId}/resources")
    @ResponseBody
    public JSONObject queryResources(@PathVariable String contractId) {
        return Response.response(contractService.queryResources(contractId));
    }


    /**
     * 查询每一个媒体分类的合同资源
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/mediaResources")
    @ResponseBody
    public JSONObject mediaResources(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(contractService.mediaResources(reqBean));
    }


    /**
     * 查询每种分类的资源个数
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/mediaTypeCount")
    @ResponseBody
    public JSONObject mediaTypeCount(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(contractService.mediaTypeCount(reqBean));
    }

    /**
     * 添加
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject addContract(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(contractService.addContract(reqBean));
    }

    /**
     * 修改
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject updateContract(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        contractService.updateContract(reqBean);
        return Response.responseSuccess();
    }


    /**
     * 新增合同资源
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/addContractResource")
    @ResponseBody
    public JSONObject addContractResource(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        contractService.addContractResource(reqBean);
        return Response.responseSuccess();
    }


    /**
     * 修改合同资源
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/updateContractResource")
    @ResponseBody
    public JSONObject updateContractResource(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        contractService.updateContractResource(reqBean);
        return Response.responseSuccess();
    }


    /**
     * 删除
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject removeContract(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        contractService.removeContract(reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/downloadModel")
    @ResponseBody
    public JSONObject downloadModel(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean, HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        contractService.downloadModel(reqBean, request, response);
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/importResources")
    @ResponseBody
    public JSONObject importResources(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
    	try{
    		contractService.contractExcel(reqBean.getInfo("contractId"),reqBean.getInfo("importFilePath"),reqBean.getInfo("mediaId"));
    		return Response.responseSuccess();
    	}catch(BusinessException e){
    		return Response.responseError(e.getData() == null ? e.getMessage() : e.getData());
    	}
    }
    
    /**
     * 删除合同资源信息
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/resource/delete/{recordId}")
    @ResponseBody
    public JSONObject removeContractResource(@PathVariable String recordId) {
        contractService.removeContractResource(recordId);
        return Response.responseSuccess();
    }

    //TODO 其他功能
}